package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink08_Keyed_State_Aggregate_1 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env
                .socketTextStream("hadoop162", 9999)
                .map(line ->{
                    String[] strings = line.split(",");
                    return new WaterSensor(
                            strings[0],
                            Long.valueOf(strings[1]),
                            Integer.valueOf(strings[2]));
                })

                .keyBy(WaterSensor::getId)

                .process(new KeyedProcessFunction<String, WaterSensor, String>() {


                    private AggregatingState<WaterSensor, Double> vcAvgState;


                    @Override
                    public void open(Configuration parameters) throws Exception {

                        vcAvgState = getRuntimeContext().getAggregatingState(
                                new AggregatingStateDescriptor<WaterSensor, Tuple2<Integer, Long>, Double>(
                                "vcAvgState", new AggregateFunction<WaterSensor, Tuple2<Integer, Long>, Double>() {
                            @Override
                            public Tuple2<Integer, Long> createAccumulator() {
                                return Tuple2.of(0, 0L);
                            }

                            @Override
                            public Tuple2<Integer, Long> add(WaterSensor value, Tuple2<Integer, Long> acc) {
                                acc.f1 += value.getVc();
                                acc.f0++;
                                return acc;
                            }

                            @Override
                            public Double getResult(Tuple2<Integer, Long> acc) {
                                return acc.f1 * 1.0 / acc.f0;
                            }

                            @Override
                            public Tuple2<Integer, Long> merge(Tuple2<Integer, Long> a, Tuple2<Integer, Long> b) {
                                return null;
                            }
                        },
                               // Types.TUPLE(Types.INT,Types.LONG)
                                TypeInformation.of(new TypeHint<Tuple2<Integer,Long>>() {})
                                ));
                    }

                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {

                        vcAvgState.add(value);
                        out.collect(ctx.getCurrentKey()+" "+vcAvgState.get());
                    }
                })

                .print();


        env.execute();
    }



}
